package org.anyin.gitee.cloud.lib.core.config;

import feign.codec.DecodeException;
import lombok.extern.slf4j.Slf4j;
import org.anyin.gitee.cloud.lib.core.enums.CommonExCodeEnum;
import org.anyin.gitee.cloud.lib.core.exceptions.CommonException;
import org.anyin.gitee.cloud.lib.core.utils.RequestIdUtil;
import org.anyin.gitee.cloud.lib.core.web.ApiResponse;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.sql.SQLException;

@Slf4j
public abstract class DefaultExceptionHandler {

    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
    public ApiResponse httpMediaTypeNotSupportedException(HttpMediaTypeNotSupportedException exception){
        log.error("不支持当前媒体类型: {}", exception.getMessage(), exception);
        String id = RequestIdUtil.getRequestId().toString();
        return ApiResponse.error(id, CommonExCodeEnum.SYSTEM_ERROR.getCode(),
                CommonExCodeEnum.SYSTEM_ERROR.getMessage());
    }

    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(DataIntegrityViolationException.class)
    public ApiResponse dataIntegrityViolationException(DataIntegrityViolationException exception){
        log.error("数据库异常: {}", exception.getMessage(), exception);
        String id = RequestIdUtil.getRequestId().toString();
        return ApiResponse.error(id, CommonExCodeEnum.SYSTEM_ERROR.getCode(),
                CommonExCodeEnum.SYSTEM_ERROR.getMessage());
    }

    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(SQLException.class)
    public ApiResponse sqlException(SQLException ex){
        log.error("数据库异常: {}", ex.getMessage(), ex);
        String id = RequestIdUtil.getRequestId().toString();
        return ApiResponse.error(id, CommonExCodeEnum.SYSTEM_ERROR.getCode(),
                CommonExCodeEnum.SYSTEM_ERROR.getMessage());
    }

    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ApiResponse methodArgumentNotValidException(MethodArgumentNotValidException ex){
        log.error("请求参数错误: {}", ex.getMessage(), ex);
        String id = RequestIdUtil.getRequestId().toString();
        String message = ex.getBindingResult().getFieldError().getDefaultMessage();
        return ApiResponse.error(id, CommonExCodeEnum.PARAM_ERROR.getCode(), message);
    }

    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(DecodeException.class)
    public ApiResponse decodeException(DecodeException ex){
        log.error("解码失败: {}", ex.getMessage());
        String id = RequestIdUtil.getRequestId().toString();
        if(ex.getCause() instanceof CommonException){
            CommonException ce = (CommonException)ex.getCause();
            return ApiResponse.error(id, ce.getErrorCode(), ce.getErrorMessage());
        }
        return ApiResponse.error(id, CommonExCodeEnum.DATA_PARSE_ERROR.getCode(), ex.getMessage());
    }

}
